from flask import Flask, jsonify, request
from flask_cors import CORS
from predict.Predictor import Predictor


entity_list = []
with open('./benchmarks/FKB/entity2id.txt') as f:
    f.readline()
    for line in f.readlines():
        entity_list.append(line.rstrip().split('\t')[0])
f.close()

predictor = Predictor()
predictor.data_load()

app = Flask(__name__)


@app.route('/knowledge-predict', methods=['POST'])
def predict():
    performance = request.form['performance']
    predict_result = {}
    if performance in entity_list:
        result = predictor.predict(performance)
        predict_result[performance] = result
    else:
        predict_result[performance] = 'invalid'

    return jsonify(predict_result)


app.route('/')
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
if __name__ == '__main__':
    app.run(debug=True, port=5000, host='0.0.0.0')
